<?php
namespace app\adapay\controller;
use AdaPaySdk\Member;
use app\adapay\model\AdminPay;
use app\adapay\model\PayRecord;
use app\AdminRest;
use app\ApiRest;
use app\massage\model\Order;
use app\massage\model\Wallet;
use app\virtual\model\PlayRecord;
use app\virtual\model\Record;
use longbingcore\wxcore\PayNotify;
use think\App;
use think\facade\Db;
use WxPayApi;


class CallBack  extends ApiRest
{

    protected $app;

    public function __construct ( App $app )
    {
        $this->app = $app;
    }



    /**
     * @author chenniang
     * @DataTime: 2022-12-08 15:33
     * @功能说明:退款回调
     */
    public function companyCallback(){

        $this->request = $this->app->request;

        $inputs = json_decode($this->request->getInput(), true);

        if(empty($inputs)){

            $inputs = $_POST;
        }
        //添加回调记录
        $callbak_model = new \app\adapay\model\Callback();

        $data = json_decode($inputs['data'],true);

        $member_model = new \app\adapay\model\Member();

        $find = $member_model->dataInfo(['status'=>0,'member_id'=>$data['member_id']]);

        if(!empty($find)){
            //成功
            if($data['audit_state']=='D'){

                $member_model->dataUpdate(['id'=>$find['id']],['status'=>1]);
            }else{

                $member_model->dataUpdate(['id'=>$find['id']],['status'=>2]);
            }
        }

        $insert = [

            'uniacid' => 666,

            'adapay_id' => $data['member_id'],

            'status' => $inputs['type'],

            'type'   => 'company',

            'text'   => serialize($inputs)
        ];

        $callbak_model->dataAdd($insert);

        $res = ['code'=>0,'msg'=>'成功'];

        echo json_encode($res);exit;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-12-08 15:33
     * @功能说明:退款回调
     */
    public function refundCallback(){

        $this->request = $this->app->request;

        $inputs = json_decode($this->request->getInput(), true);

        if(empty($inputs)){

            $inputs = $_POST;
        }
        //添加回调记录
        $callbak_model = new \app\adapay\model\Callback();

        $data = json_decode($inputs['data'],true);

        $pay_record_model = new PayRecord();

        $dis = [

            'adapay_id' => $data['id']
        ];

        $find = $pay_record_model->dataInfo($dis);

        if(empty($find)){

            $insert = [

                'uniacid' => 666,

                'adapay_id' => $data['id'],

                'status' => $inputs['type'],

                'type'   => 'refund',

                'text'   => serialize($inputs)
            ];

            $callbak_model->dataAdd($insert);
        }

        $res = ['code'=>0,'msg'=>'成功'];

        echo json_encode($res);exit;

    }

    /**
     * @author chenniang
     * @DataTime: 2023-08-12 16:16
     * @功能说明:支付回调
     */
    public function payCallback(){

        $this->request = $this->app->request;

        $inputs = json_decode($this->request->getInput(), true);

        if(empty($inputs)){

            $inputs = $_POST;
        }
        //添加回调记录
        $callbak_model = new \app\adapay\model\Callback();

        $data = json_decode($inputs['data'],true);
        //回调成功
        if($data['status']=='succeeded'){

            $pay_record_model = new PayRecord();

            $dis = [

                'adapay_id' => $data['id']
            ];

            $record = $pay_record_model->dataInfo($dis);

            if(!empty($record)){

                if( $record['type']=='Balance'){

                    $order_model = new \app\massage\model\BalanceOrder();

                    $res    = $order_model->orderResult($record['order_code'],$data['id']);

                    return $res;

                }elseif($record['type']=='Massage'){

                    $order_model = new \app\massage\model\Order();

                    $res    = $order_model->orderResult($record['order_code'],$data['id']);

                    return $res;

                }elseif($record['type']=='MassageUp'){

                    $order_model = new \app\massage\model\UpOrderList();

                    $res    = $order_model->orderResult($record['order_code'],$data['id']);

                    return $res;

                }elseif($record['type']=='Adapay'){

                    $order_model = new AdminPay();

                    $res    = $order_model->orderResult($record['order_code'],$data['id']);

                    return $res;
                }

            }

        }

        $dis = [

            'adapay_id' => $data['id']
        ];

        $find = $callbak_model->dataInfo($dis);

        if(empty($find)){

            $insert = [

                'uniacid' => 666,

                'adapay_id' => $data['id'],

                'status' => $inputs['type'],

                'type'   => 'pay',

                'text'   => serialize($inputs)
            ];

            $callbak_model->dataAdd($insert);
        }

        $res = ['code'=>0,'msg'=>'成功'];

        echo json_encode($res);exit;

    }


    /**
     * @author chenniang
     * @DataTime: 2023-08-18 18:35
     * @功能说明:打款回调
     */
    public function walletCallback(){

        $this->request = $this->app->request;

        $inputs = json_decode($this->request->getInput(), true);

        if(empty($inputs)){

            $inputs = $_POST;
        }
        //添加回调记录
        $callbak_model = new \app\adapay\model\Callback();

        $data = json_decode($inputs['data'],true);
        //回调成功
        if($data['status']=='succeeded'){

            $wallet_model  = new Wallet();

            $dis = [

                'payment_no' => $data['id']
            ];

            $record = $wallet_model->dataInfo($dis);

            if(!empty($record)){

                $wallet_model->dataUpdate(['id'=>$record['id']],['status'=>2]);
            }
        }

        $insert = [

            'uniacid' => 666,

            'adapay_id' => $data['id'],

            'status' => $inputs['type'],

            'type'   => 'wallet',

            'text'   => serialize($inputs)
        ];

        $callbak_model->dataAdd($insert);

        $res = ['code'=>0,'msg'=>'成功'];

        echo json_encode($res);exit;


    }



}
